package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavoerMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜品
 */
@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavoerMapper dishFlavoerMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;



    /**
     * 新增菜品
     * @param dto
     */
    @Override
    @Transactional
    public void newDish(DishDTO dto) {
         Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.insert(dish);
         Long dishId = dish.getId();

         List<DishFlavor> flavors = dto.getFlavors();
         if(flavors!=null&&flavors.size()>0){
             flavors.forEach(dishFlavor -> {
                 dishFlavor.setDishId(dishId);
             });
         }
        dishFlavoerMapper.newFlavoer(flavors);

    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult DishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

    if(dishPageQueryDTO.getName()!=null){
        dishPageQueryDTO.setName(dishPageQueryDTO.getName().trim());
    }
     Page<DishVO> page =  dishMapper.DishPage(dishPageQueryDTO);
         long total = page.getTotal();
         List<DishVO> result = page.getResult();


        return new PageResult(total,result);
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void DeleteDish(List<Long> ids) {

        for (Long id : ids) {
            Dish dish =  dishMapper.getById(id);
            log.info("获得菜品id：{}",id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> setmealId = setmealDishMapper.getsetmealDishById(ids);
        log.info("获得套餐：{}",ids);
        if(setmealId.size()>0&& setmealId!=null){
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        for (Long id : ids) {
            log.info("删除菜品：{}",id);
            dishMapper.deletebyId(id);
            log.info("删除口味：{}",id);
            dishFlavoerMapper.deleteflavoerbyId(id);
        }
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO dishById(Long id) {
        Dish dish =  dishMapper.getById(id);
       List<DishFlavor> dishFlavors= dishFlavoerMapper.getFlavorById(id);
         DishVO dishVO = DishVO.builder().build();
         BeanUtils.copyProperties(dish,dishVO);
         dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dto
     */
    @Override
    public void update(DishDTO dto) {
         Dish dish = Dish.builder().build();
         BeanUtils.copyProperties(dto,dish);
         dishMapper.update(dish);
         dishFlavoerMapper.deleteflavoerbyId(dto.getId());
        List<DishFlavor> flavors = dto.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dto.getId());
            });
        }
        dishFlavoerMapper.newFlavoer(flavors);
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Integer status, Long id) {

         Dish dish = Dish.builder().build();
         dish.setStatus(status);
         dish.setId(id);
        dishMapper.update(dish);

    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> dishByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品：{},{}",categoryId);
      List<Dish> dish =   dishMapper.dishByCategoryId(categoryId);
        return dish;
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavoerMapper.getFlavorById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
